﻿// 提交的地址
var JOIN_US__UPDATE_URL = "/Allspark/next";
console.log(`
产品造就生活,
代码编制未来,
allSpark工作室,
欢迎你的加入!`);
//待发送的数据
var joinData = {
    name: "",
    classroom: "",
    school: "",
    tel: "",
    introduce: "",
    direction: [],
    group: [],
};
//各种dom变量
var joinUsName = document.getElementById("joinUsName");
var joinUsClass = document.getElementById("joinUsClass");
var joinUsSchool = document.getElementById("joinUsSchool");
var joinUsTel = document.getElementById("joinUsTel");
var joinUsIntroduce = document.getElementById("joinUsIntroduce");
var joinUsNameMsg = document.getElementById("joinUsNameMsg");
var joinUsClassMsg = document.getElementById("joinUsClassMsg");
var joinUsSchoolMsg = document.getElementById("joinUsSchoolMsg");
var joinUsTelMsg = document.getElementById("joinUsTelMsg");
var joinUsIntroduceMsg = document.getElementById("joinUsIntroduceMsg");
var joinUsSubmit = document.getElementById("joinUsSubmit");
var joinUsUpdateMsg = document.getElementById("joinUsUpdateMsg");
//清除表单的输入值
function joinUsClear() {
    var arr = document.getElementsByTagName("input");
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].type === "button") {
            arr[i].parentNode.classList.remove("active");
        }
        if (arr[i].type === "text") {
            arr[i].value = "";
        }
    }
    console.log(arr);
    document.getElementById("joinUsIntroduce").value = "";
}
//用户点击输入错误的表单项目，表单自动清除错误信息
function joinUsClearError(e) {
    switch (e.id) {
        case "joinUsName":
            joinUsNameMsg.innerText = "";
            break;
        case "joinUsClass":
            joinUsClassMsg.innerText = "";
            break;
        case "joinUsSchool":
            joinUsSchoolMsg.innerText = "";
            break;
        case "joinUsTel":
            joinUsTelMsg.innerText = "";
            break;
        case "joinUsIntroduce":
            joinUsIntroduceMsg.innerText = "";
            break;
    }
}
// 检测输入表单是否合法
function joinUsTest() {
    var nameRex = /^[\u4e00-\u9fa5]{2,10}$/;
    var classRex = /[\u4e00-\u9fa5]{2,10}\d{0,5}/;
    if (joinUsName.value.length == 0) {
        joinUsNameMsg.innerText = "名称不能为空";
        return false;
    }
    if (joinUsSchool.value.length == 0) {
        joinUsSchoolMsg.innerText = "学校不能为空";
        return false;
    }
    if (joinUsClass.value.length == 0) {
        joinUsClassMsg.innerText = "班级不能为空";
        return false;
    }
    if (joinUsTel.value.length == 0) {
        joinUsTelMsg.innerText = "电话不能为空";
        return false;
    }
    if (joinUsIntroduce.value.length == 0) {
        joinUsIntroduceMsg.innerText = "描述不能为空";
        return false;
    }
    if (!nameRex.test(joinUsName.value)) {
        joinUsNameMsg.innerText = "请输入中文";
        return false;
    }
    if (joinUsName.value.length > 12 || joinUsName.value.length < 1) {
        joinUsNameMsg.innerText = "请输入正确的姓名格式";
        return false;
    }
    if (!nameRex.test(joinUsSchool.value)) {
        joinUsSchoolMsg.innerText = "请使用中文";
        return false;
    }
    if (!classRex.test(joinUsClass.value)) {
        joinUsClassMsg.innerText = "输入不合法";
        return false;
    }
    var telRex = /[0-9]{8,11}/
    if (!telRex.test(joinUsTel.value)) {
        joinUsTelMsg.innerText = "电话号码应为数字且长度小于12";
        return false;
    }
    if (joinUsIntroduce.value.length > 100) {
        joinUsIntroduceMsg.innerText = "描述应少于100字";
        return false;
    }
    return true;
}
//表单项 点击的上升动画效果
function joinUsTableAn(e) {
    e.children[1].focus();
    e.children[0].classList.add("active");
}
//表单项 获得焦点的上升动画效果
function joinUsTableChildAn(e) {
    e.parentNode.children[1].focus();
    e.parentNode.children[0].classList.add("active");
}
var isSending = false;
//刷新之后清空表单输入 防止文字和表单项标题重合
window.onunload = function (event) {
    var tempUnloadDH = document.getElementsByTagName("input");
    for (var tempUnloadI = 0; tempUnloadI < tempUnloadDH.length; tempUnloadI++) {
        tempUnloadDH[tempUnloadI].value = "";
    }
    document.getElementById("joinUsIntroduce").value = "";
};
//提交表单
joinUsSubmit.onclick = function joinUsSubmitData() {
    var pos = new XMLHttpRequest();
    pos.open("POST", JOIN_US__UPDATE_URL, true);
    if (joinUsTest() && !isSending) {
        joinData.name = joinUsName.value;
        joinData.classroom = joinUsClass.value;
        joinData.school = joinUsSchool.value;
        joinData.tel = joinUsTel.value;
        joinData.introduce = joinUsIntroduce.value;
        pos.onreadystatechange = function () {
            if (pos.status == 200 && pos.readyState >= 3) {
                isSending = false;
                joinUsSubmit.classList.remove("update");
                switch (pos.responseText) {
                    case "1":
                        joinUsUpdateMsg.innerText = "发送成功";
                        break;
                    case "-1":
                        joinUsUpdateMsg.innerText = "名称重复";
                        break;
                    case "0":
                        joinUsUpdateMsg.innerText = "发送失败";
                        break;
                }
                setTimeout(() => {
                    joinUsUpdateMsg.innerText = "";
                }, 3000);
            }
        }
        pos.setRequestHeader("Content-type", "application/json");
        pos.send(JSON.stringify(joinData));
        joinUsSubmit.classList.add("update");
        joinUsUpdateMsg.innerText = "发送中...";
        isSending = true;
        joinUsClear();
        joinData = {
            name: "",
            classroom: "",
            school: "",
            tel: "",
            introduce: "",
            direction: [],
            group: [],
        }
    }
}
//提交动画效果
joinUsSubmit.onmousedown = function () {
    joinUsSubmit.classList.add("active");
}
document.onmouseup = function () {
    joinUsSubmit.classList.remove("active");
}
//选择方向的数据修改
function chooseDirection(e) {
    var p = joinData.direction.indexOf(e.value);
    if (p == -1) {
        joinData.direction.push(e.value);
        e.classList.add("active");
    } else {
        joinData.direction.splice(p, 1);
        e.classList.remove("active");
    }
}
//选择组别的数据修改
function chooseGroup(e) {
    var p = joinData.group.indexOf(e.value);
    if (p == -1) {
        joinData.group.push(e.value);
        e.classList.add("active");
    } else {
        joinData.group.splice(p, 1);
        e.classList.remove("active");
    }
}
var navList = document.querySelector('.navList');
var navLists = document.querySelectorAll('.navList_3');
var chooseLists = document.querySelectorAll('.chooseList');

for (var i = 0; i < chooseLists.length; i++) {

    chooseLists[i].onmouseover = function () {
        this.children[1].style.display = 'block';
    }
    chooseLists[i].onmouseout = function () {
        this.children[1].style.display = 'none';
    }

}